#!/usr/bin/perl
#vol_delete.cgi

require './diskman-lib.pl';

&ReadParse();

my ($err_msg, $log_ev, $log_msg, $log_pri);

$err_msg="";
$log_ev="";

my $volname = $in{'volume'};
if ($volname eq "") {
	$err_msg = $text{"err_cgiparam"};
	goto ERROR_OUT;
}
my $mkfs_lock;
$mkfs_lock = check_lock();
if ($mkfs_lock) {
	$err_msg = $text{"err_mkfslocked"};
	goto ERROR_OUT;
}
my (@rdinfo, $r, $rr, @lvinfo);
@rdinfo = new_get_check_raid_info();
$found = 0;
foreach $r (@rdinfo) {
	if ($r->{'name'} eq $volname) {
		$rr = $r;
		$found = 1;
		last;
	}
}
if (!$found) {
	$err_msg = $text{"err_configchanged"};
	goto ERROR_OUT;
}

my ($out, $mddev, @rdev);
$volume_name = $rr->{'volname'};
my $sdev = $rr->{'sdev'};;
my @pvinfo = get_pv_info();
my $pvfound = 0;
my ($sdpv, $pvv);
foreach $pvv (@pvinfo){
	if($pvv->{'pv'} eq $sdev){
		$pvfound = 1;
		$sdpv = $pvv;
		last;
	}
}
if($pvfound){
	if($sdpv->{'vg'}){
		$err_msg = $text{'err_raidinvg'};
		goto ERROR_OUT;
	}
	else{
                $rmpv = `/sbin/lvm pvremove $sdev -ff -y 2>/dev/null`;
                $rc1 = $?>>8;
                if($rc1){
                        $err_msg = $text{'err_pvremove'};
                        goto ERROR_OUT;
                }	
	}
}
$err_msg = $volume_name.$volname; #.$sdev;
#goto ERROR_OUT;

my ($d, $p);
#check whether the status of volume
if ($rr->{'status'} eq 'new') {
	#raid stop
}
elsif (($rr->{'status'} eq 'Online') || ($rr->{'status'} eq 'OFFLINE') || ($rr->{'status'} eq 'CRITICAL') || ($rr->{'status'} eq 'REBUILDING') || ($rr->{'status'} eq 'INITIALIZING')) {
        	$uid = $rr->{'name'};
        	system("/usr/bin/cli logdrv -a del -l $volname");
        	if($?){
			$err_msg = "Delete RAID failed!";
			goto ERROR_OUT;
        	}
}
else {  
	$err_msg = $text{"err_configchanged"};
	goto ERROR_OUT;	
}

$log_ev = $text{'log_ev_delvolume_ok'};
$log_msg= &text('log_msg_delvolume_ok', $volname);
nas_syslog($text{'my_log_minor'}, 'naslog_info', $log_ev, $log_msg);

redirect("index.cgi");

ERROR_OUT:
&error_setup($text{'del_err_title'});
&error($err_msg);
